package com.example.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.NewsDetailEntity
import com.example.base.entity.NewsDetailEntityResult
import com.example.base.entity.NewsEntityResult
import com.example.base.entity.ParamNews
import com.example.base.mvvm.BaseViewModel
import com.example.base.net.ApiServer
import kotlinx.coroutines.launch

class MainViewModel:BaseViewModel() {
    //列表返回值
    val newsSuccess = MutableLiveData<NewsEntityResult>()
    val newsFail = MutableLiveData<String>()
    fun news(){
        viewModelScope.launch {
            try {
                newsSuccess.value = apiServer.news()
            }catch (e:Exception){
                newsFail.value = e.message
            }
        }
    }
    //详情返回值
    val detailSuccess = MutableLiveData<NewsDetailEntityResult>()
    val detailFail = MutableLiveData<String>()
    fun detail(id:Int){
        viewModelScope.launch {
            try {
                detailSuccess.value = apiServer.newsDetail(id)
            }catch (e:Exception){
                detailFail.value = e.message
            }
        }
    }
    //修改返回值
    val updateSuccess = MutableLiveData<NewsDetailEntityResult>()
    val updateFail = MutableLiveData<String>()
    fun update(id:Int,body:ParamNews){
        viewModelScope.launch {
            try {
                updateSuccess.value = apiServer.updateNews(id,body)
            }catch (e:Exception){
                updateFail.value = e.message
            }
        }
    }
}